<template>
    <!--价格、库存-->
    <kdx-modal-frame v-model="value" title="变更归还时间" :width="500" @on-cancel="handleCancel" @on-ok="handleOk">
        <Form ref="form_info" :model="model" :rules="rules" :label-width="120" class="edit-information-modal">
            <FormItem label="归还时间" prop="lease_return_at">
                <DatePicker
                    v-model="model.lease_return_at"
                    type="date"
                    placeholder="归还时间"
                    format="yyyy-MM-dd"
                    class="width-340"
                ></DatePicker>
            </FormItem>
        </Form>
    </kdx-modal-frame>
</template>

<script>

    export default {
        name: "SetReturnAt",
        props: {
            lease_return_at: {
                type: [String]
            },
            id: {
                type: [Number, String]
            },
        },
        data() {
            return {
                value: false,
                model: {
                    lease_return_at: ''
                },
                rules: {
                    lease_return_at: [{
                        required: true,
                        message: '归还时间必填'
                    }],
                }
            };
        },
        methods: {
            setValue() {
                this.value = !this.value;
                if (this.value) {
                    this.model = {
                        lease_return_at: this.$moment.unix(this.lease_return_at).format("YYYY-MM-DD"),
                    };
                }
            },
            handleCancel() {
                this.setValue();
            },
            handleOk() {
                // 调用接口
                this.$refs['form_info'].validate(valid => {
                    if (valid) {
                        let lease_return_at = this.$moment.unix(this.model.lease_return_at).unix() / 1000 + 43200;
                        let params = {
                            order_id: this.id, 
                            order_goods_id: 'all', 
                            return_at: lease_return_at
                        };
                        this.$api.orderApi.setReturnAt(params)
                            .then(res => {
                                if (res.error === 0) {
                                    !this.isExternal && this.setValue();
                                    this.$emit('on-refresh');
                                    location. reload()
                                }
                            });
                    }
                });
            },
        }
    };
</script>

<style scoped lang="scss">
    .edit-information-modal {
        padding-top: 40px;
    }
</style>
